package com.genfu.reform.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.genfu.reform.model.UserInfo;
import com.genfu.reform.service.LoginService;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.ValidationAwareSupport;

@InterceptorRefs({ @InterceptorRef("defaultStack") })
@ResultPath("/")
@Results({
		@Result(name = "login", location = "login.jsp"),
		@Result(name = "success", location = "index.jsp"),
		@Result(name = "success1", type = "redirect", params = {
				"location", "/application.jsp" }) })
public class LoginController extends ValidationAwareSupport implements
		ModelDriven<Object>, Validateable, SessionAware, ParameterAware,
		ServletRequestAware, ServletResponseAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private LoginService loginService;
	private UserInfo userInfo = new UserInfo();
	private Map<String, Object> session;
	private Map<String, String[]> parameters;

	public LoginController(LoginService theService) {
		this.loginService = theService;
	}

	public String execute() {
		String result = "login";
		return result;

	}

	public String index() {
		loginService.authentication(getUserInfo());
		// session.put(GenfuAuthenticationInterceptor.USER_SESSION_KEY,
		// userInfo);
		return "login";

	}

	// @Action(interceptorRefs = @InterceptorRef("servletConfig"))
	public String create() {
		System.out.println("Controller...create...");
		loginService.authentication(userInfo);

		addActionMessage("login failure");
		String result = "login";
		if ("123456".equals(userInfo.getUserCode())) {
			session.put(GenfuAuthenticationInterceptor.USER_SESSION_KEY,
					userInfo);
			result = "success1";
		}

		return result;
	}

	public UserInfo getUserInfo() {
		System.out.println("Controller...getUserInfo...");
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		System.out.println("Controller...setUserInfo...");
		this.userInfo = userInfo;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		System.out.println("Controller...setServletResponse...");

	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		System.out.println("Controller...setServletRequest...");
	}

	@Override
	public void setSession(Map<String, Object> session) {
		System.out.println("Controller...setSession...");
		if (null != this.session) {
			session = this.session;
		} else {
			this.session = session;
		}
	}

	@Override
	public void validate() {
		System.out.println("Controller...validate...");
		loginService.authentication(userInfo);

		addActionMessage("login failure");
		if ("123456".equals(userInfo.getUserCode())) {
		}
	}

	@Override
	public Object getModel() {
		System.out.println("Controller...getModel...");
		return null;
	}

	@Override
	public void setParameters(Map<String, String[]> arg0) {
		System.out.println("Controller...setParameters..." + arg0.size());
		if (null != this.parameters) {
			arg0 = this.parameters;
		} else {
			this.parameters = arg0;
		}
	}
}
